package org.example.controller;

import lombok.AllArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.example.dao.Comment;
import org.example.dao.Dynamic;
import org.example.dao.Result;
import org.example.enums.responseEnum;
import org.example.service.DynamicService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@AllArgsConstructor
@RestController
@RequestMapping("/dynamic")
public class DynamicController {
    private final DynamicService dynamicService;


    @PostMapping("/addDynamic")
    public Result addComment(@RequestBody Dynamic dynamic) {
        Integer i = dynamicService.addDynamic(dynamic);
        if (i >= 0) {
            return new Result(200, "添加成功", i);
        } else if (i == -1) {
            return new Result(500, "添加失败", null);
        } else {
            return new Result(400, "uid或内容为空", null);
        }
    }

    //getDynamicList
    @GetMapping("/getDynamicList")
    public Result getDynamicList() {
        List<Dynamic> dynamicList = dynamicService.getDynamicList();
        return new Result(200, "获取成功", dynamicList);
    }

    @GetMapping("/getDynamicListByPage")
    public Result getDynamicListByPage(@RequestParam("page") Integer page, @RequestParam("size") Integer size) {
//        try {
//            Thread.sleep(1000);
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
        List<Dynamic> dynamicList = dynamicService.getDynamicListByPage(page, size);
        if (dynamicList == null) {
            return new Result(500, "获取失败", null);
        } else {
            return new Result(200, String.valueOf(dynamicService.getMaxPageNumber(size)), dynamicList);
        }
    }

}
